package com.jquest.editor;

import com.jquest.editor.components.PropertySidePanel;
import com.jquest.editor.components.SidePanel;
import org.newdawn.slick.*;

import javax.swing.*;
import javax.imageio.ImageIO;

import com.jquest.editor.components.ImageList;
import com.jquest.editor.components.StatusBar;
import com.jquest.misc.Tileset;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Johan
 * Date: 2009-dec-11
 * Time: 21:20:21
 * To change this template use File | Settings | File Templates.
 */
public class Editor extends JFrame implements ActionListener {
    //ToDo: Code cleanup, bind these visual components to the editor itself , fix UI to allow smooth resizing
    CanvasGameContainer cgc;

    SidePanel sidePanel;
    PropertySidePanel propertyPanel;

    StatusBar status;

    JLabel coords;

    JToolBar toolbar;
    JButton newMap;
    JButton saveMap;
    JMenuBar menu;
    JMenu fileMenu;
    RenderCanvas canvas;

    public SidePanel getSidePanel() {
        return sidePanel;
    }

    public static void main(String[] args) {

            System.setProperty("java.library.path", "./lib/native");
            System.loadLibrary("lwjgl");
            System.loadLibrary("OpenAL32");
            System.loadLibrary("jinput-raw");
            System.loadLibrary("jinput-dx8");

        try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch(Exception e) {

        }
        new Editor();
    }

    public Editor() {
        super("JQuest Map Editor");
        try {
            initializeComponents();

        } catch(Exception e) { e.printStackTrace(); }

    }

    public void initializeComponents() throws Exception {
        canvas = new RenderCanvas(this);
        cgc = new CanvasGameContainer(canvas);
        toolbar = new JToolBar();
        coords = new JLabel("Tile");
        newMap = new JButton(new ImageIcon(ImageIO.read(new File("./res/icons/new.PNG"))));
        saveMap = new JButton(new ImageIcon(ImageIO.read(new File("./res/icons/save.PNG"))));

        sidePanel = new SidePanel();
        propertyPanel = new PropertySidePanel();

        status = new StatusBar();
        //status.setPreferredSize(new Dimension(5, 25));
        status.setBorder( BorderFactory.createLoweredBevelBorder() );

        newMap.setActionCommand("new");
        saveMap.setActionCommand("save");
        saveMap.addActionListener(this);

        menu = new JMenuBar();
        fileMenu = new JMenu("File");
       

        menu.add(fileMenu);

        toolbar.setFloatable(false);
        toolbar.add(newMap);
        toolbar.add(saveMap);

        cgc.getContainer().setTargetFrameRate(60);
        cgc.setSize(400, 400);

        this.setJMenuBar(menu);
        this.add(toolbar, BorderLayout.PAGE_START);
        this.add(cgc, BorderLayout.CENTER);
        this.add(sidePanel, BorderLayout.WEST);
        this.add(propertyPanel, BorderLayout.EAST);
        this.add(status, BorderLayout.SOUTH);
        this.setSize(800, 600);
        this.setVisible(true);
        cgc.start();
    }

    public void setPropertyObject(Object object) {
        propertyPanel.setObject(object);
    }

    public StatusBar getStatusBar() {
        return status;
    }

    public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();
        if(action.equals("save")) {
           try {
               canvas.getMap().save("./res/maps/test.map");
               System.out.println("SAVED MAP!");
           } catch(Exception a) {
                a.printStackTrace();   
           }
        }
    }

    public ImageList getTileList() {
        return sidePanel.imageList;
    }

}
